package cs294s.liveradar;



import cs294s.liveradar.service.BlipManager;


import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class HomeScreen extends Activity {

	private static final int STOPSPLASH = 0;
	private static final long SPLASHTIME = 1000;

	private ImageView splash; 
	private Button mapButton;
	private Button blipCreateButton;
	private Button blipListButton;
	private Button filterCreateButton;
	private Button filterListButton;
	private Button settingsButton;

	BlipManager blipManager;



	private Handler splashHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOPSPLASH:
				splash.setVisibility(View.GONE);
				break;
			}
			super.handleMessage(msg);
		}
	}; 
	
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  //ignore orientation change
	  super.onConfigurationChanged(newConfig);
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		splash();
		setupUI();
		blipManager = BlipManager.getInstance(this);

	}

	public void onResume() {
		super.onResume();
		blipManager.checkLogin();
	}
	public void splash() {
		splash = (ImageView) findViewById(R.id.splashscreen);
		Message msg = new Message();
		msg.what = STOPSPLASH;
		splashHandler.sendMessageDelayed(msg, SPLASHTIME);
	}

	public void setupUI() {


		mapButton = (Button) findViewById(R.id.mapButton);
		blipCreateButton = (Button) findViewById(R.id.blipCreateButton);
		blipListButton = (Button) findViewById(R.id.blipListButton);
		filterCreateButton = (Button) findViewById(R.id.filterCreateButton);
		filterListButton = (Button) findViewById(R.id.filterListButton);
		settingsButton = (Button) findViewById(R.id.settingsButton);

		mapButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i = new Intent(getBaseContext(), RadarMap.class);
				startActivity(i);
			}
		});

		blipCreateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i = new Intent(getBaseContext(), BlipCreateForm.class);
				startActivity(i);
			}
		});
		blipListButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i = new Intent(getBaseContext(), BlipList.class);
				startActivity(i);
			}
		});
		filterCreateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i= new Intent(getBaseContext(), FilterCreateForm.class);
				startActivity(i);
			}
		});
		filterListButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i = new Intent(getBaseContext(), FilterList.class);
				startActivity(i);
			}
		});
		settingsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				Intent i = new Intent(getBaseContext(), Preferences.class);
				startActivity(i);
			}
		});
	}

}
